﻿/* Copyright 2008-10 Jarrett Vance
* see http://www.codeplex.com/blogsvc/license */
namespace AtomSite.WebCore
{
  using System.Web.Mvc;
  using AtomSite.Domain;
  public partial class AdminController : BaseController
  {
    [ScopeAuthorize]
    public ViewResult Annotations(string workspace, string collection, string filter, int? page, int? pageSize)
    {
      var annotations = GetAnnotations(workspace, collection, filter, page, pageSize);
      var m = new AdminAnnotationsModel() { Annotations = annotations };
      m.AllCount = AtomEntryRepository.GetEntriesCount(SelectionCriteria.AnnotationsAll(workspace, collection));
      m.PublishedCount = AtomEntryRepository.GetEntriesCount(SelectionCriteria.AnnotationsPublished(workspace, collection));
      m.UnapprovedCount = AtomEntryRepository.GetEntriesCount(SelectionCriteria.AnnotationsUnapproved(workspace, collection));
      m.SpamCount = AtomEntryRepository.GetEntriesCount(SelectionCriteria.AnnotationsSpam(workspace, collection));
      m.Filter = filter;

      return View("AdminAnnotations", "Admin", m);
    }

    protected IPagedList<AtomEntry> GetAnnotations(string workspace, string collection, string filter, int? page, int? pageSize)
    {
      int pageIndex = page - 1 ?? 0;
      int pSize = pageSize ?? AppService.GetCollection().PageSize;

      var annotations = AtomPubService.GetEntries(
        filter == "pending" ? SelectionCriteria.AnnotationsPending(workspace, collection) :
        filter == "published" ? SelectionCriteria.AnnotationsPublished(workspace, collection) :
        filter == "spam" ? SelectionCriteria.AnnotationsSpam(workspace, collection) :
        filter == "unapproved" ? SelectionCriteria.AnnotationsUnapproved(workspace, collection) :
        SelectionCriteria.AnnotationsAll(workspace, collection), pageIndex, pSize);

      foreach (AtomEntry a in annotations)
      {
        a.SetValue<string>(Atom.SvcNs + "parentTitle", AtomEntryRepository.GetEntry(a.Id.GetParentId()).Title.ToString());
      }
      return annotations;
    }
  }
}
